package com.zhuoxin.dao.system;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import com.zhuoxin.bean.system.SysAccess;
import com.zhuoxin.bean.system.SysAccessSO;
import com.zhuoxin.dao.BaseDAOImpl;

public class SysAccessDAOImpl extends BaseDAOImpl<SysAccess, SysAccessSO> implements SysAccessDAO {

	@Override
	public String getHqlWhere(SysAccessSO so) {
		StringBuffer where=new StringBuffer();
		
		if(so.getSort()!=null){
			where.append(" and sort=:sort");
		}
		
		if(so.getDataId()!=null){
			where.append(" and dataId=:dataId");
		}
		
		return where.toString();
	}

	@Override
	public void setCriteriaParams(Criteria criteria, SysAccessSO so) {
		if(StringUtils.isNotEmpty(so.getName())){
			criteria.add(Restrictions.ilike("name", "%"+StringUtils.trim(so.getName())+"%"));
		}
		
		if(so.getSort()!=null){
			criteria.add(Restrictions.eq("sort", so.getSort()));
		}
		
		if(so.getDataId()!=null){
			criteria.add(Restrictions.eq("dataId", so.getDataId()));
		}
	}

	@Override
	public void setQueryParams(Query query, SysAccessSO so) {
		if(so.getSort()!=null){
			query.setParameter("sort", so.getSort());
		}
		
		if(so.getDataId()!=null){
			query.setParameter("dataId", so.getDataId());
		}
	}

	@Override
	public void switchVO2PO(SysAccess po, SysAccessSO so) {
		if(StringUtils.isNotEmpty(so.getName())){
			po.setName(StringUtils.trim(so.getName()));
		}
		
		if(StringUtils.isNotEmpty(so.getRealname())){
			po.setRealname(StringUtils.trim(so.getRealname()));
		}
		
		if(StringUtils.isNotEmpty(so.getPath())){
			po.setPath(StringUtils.trim(so.getPath()));
		}
		
		if(so.getDataId()!=null){
			po.setDataId(so.getDataId());
		}
		
		if(so.getSort()!=null){
			po.setSort(so.getSort());
		}
		
		if(so.getFkUserId()!=null){
			po.setFkUserId(so.getFkUserId());
		}
		
		if(StringUtils.isNotEmpty(so.getProvider())){
			po.setProvider(StringUtils.trim(so.getProvider()));
		}
		
		if(StringUtils.isNotEmpty(so.getExtend())){
			po.setExtend(StringUtils.trim(so.getExtend()));
		}
		if(StringUtils.isNotEmpty(so.getContent())){
			po.setContent(StringUtils.trim(so.getContent()));
		}
		
		if(so.getLook()!=null){
			po.setLook(so.getLook());
		}
		
		if(so.getDown()!=null){
			po.setDown(so.getDown());
		}
		
		if(StringUtils.isNotEmpty(so.getSize())){
			po.setSize(StringUtils.trim(so.getSize()));
		}
		
	}
	
	public void delete(SysAccessSO so){
		Session session=getSessionFactory().getCurrentSession();
		Transaction transaction=session.beginTransaction();
		String hql="delete from SysAccess where 1=1"+getHqlWhere(so);
		Query query=session.createQuery(hql);
		setQueryParams(query, so);
		query.executeUpdate();
		transaction.commit();
	}


}
